package Json_demo;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

public class Json_save_ListOBJ {

	public static void main(String[] args) {
		List<Teacher> ListTe = new ArrayList<Teacher>();
	Teacher Gv01 = new Teacher();
	Gv01.setRollNo("gv01");
	Gv01.setFullName("Tran a");
	Gv01.setMon("Toan");
	Gv01.setDiemSo(8.0);
	Teacher Gv02 = new Teacher();
	Gv02.setRollNo("gv01");
	Gv02.setFullName("Tran f");
	Gv02.setMon("Toan");
	Gv02.setDiemSo(9.0);
	Teacher Gv03 = new Teacher();
	Gv03.setRollNo("gv01");
	Gv03.setFullName("Tran e");
	Gv03.setMon("Toan");
	Gv03.setDiemSo(7.0);
	Teacher Gv04 = new Teacher();
	Gv04.setRollNo("gv01");
	Gv04.setFullName("Tran d");
	Gv04.setMon("Toan");
	Gv04.setDiemSo(5.0);
	ListTe.add(Gv01);
	ListTe.add(Gv02);
	ListTe.add(Gv03);
	ListTe.add(Gv04);
	// su dung code co san tao cua so luu tru file Json
	JFileChooser objFile = new JFileChooser();
    objFile.setCurrentDirectory(new java.io.File("."));
    objFile.setDialogTitle("Chọn file: ");
    objFile.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
    String[] EXTENSION = new String[] { "json" };
    FileNameExtensionFilter filter = new FileNameExtensionFilter("JSON files", EXTENSION);
    objFile.setFileFilter(filter);
    objFile.setMultiSelectionEnabled(false);

    int returnVal = objFile.showSaveDialog(null);
    String ParentPath = "";
    String FileName = "";
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        // ParentPath = objFile.getSelectedFile().getParent();
        FileName = objFile.getSelectedFile().getPath();
        
        //khoi tao thu vien objmapper cua Json
        ObjectMapper objma = new ObjectMapper();
        // chuyen doi du lieu cua obj thanh dang file cau truc Json
        
        try {
			objma.writeValue(new File(FileName), ListTe);
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
}
